var zxdata = null;
var mbList = null;
var sjcGnList = null;
var tburl="/portal/user/layout/tbyy.do" ;

$(function(){

     $("#endMarker").text("布局设置");
     $("#list-Gn .active").removeClass("active");
     $("#itemLayout").addClass("active");

     zxdata = JSON.parse($("#zxdata").val());
     mbList = JSON.parse($("#mbList").val());
     sjcGnList = JSON.parse($("#sjcGnList").val());

     initBody();

     $("input[name='zxlx']").click(function(){
         showBox(this.value)
     });

    $(".index-menu .tab-one").click(function(){
        $(this).addClass("active").siblings(".tab-one").removeClass("active");
        var dataId = $(this).attr("data-tab");
        $(".index-menu .tab-button-group").hide();
        $(".index-menu #"+dataId).fadeIn();
    });

    $(".index-menu .tab-one").eq(0).click();

});


function initBody(){

    var zxlx_dm = zxdata.zxlx_dm;
    $("input[name='zxlx'][value='"+zxlx_dm+"']").attr("checked",true);
    showBox($("input[name='zxlx']:checked").val());

    $("#mbSelect").empty();
    for(var i=0;i<mbList.length;i++){
         $("#mbSelect").append("<option value='"+mbList[i]["MBID"]+"' class='title-s'>"+mbList[i]["MBMC"]+"</option>");
    }
    $("#mbSelect option[value='"+zxdata["mbid"]+"']").attr("selected",true);
    initBjTable();

}

function showBox(zxlx){

    if (zxlx==1){
        $("#scjbox").slideDown();
        $("#tbxxbox").slideUp();
    }else if(zxlx==2){
        $("#scjbox").slideUp();
        $("#tbxxbox").slideDown();
    }else if(zxlx==3){
        $("#scjbox").slideDown();
        $("#tbxxbox").slideDown();
    }

}

function initBjTable(){

    var mbid = $("#mbSelect").val();

    function getWidth(cols){
        if(cols=="3"){
            return '33%'
        }
        if(cols=="4"){
            return '25%'
        }
    }

    function initTable(bjList,rownum,colnum){

        var table="";
        var thead="";
        var tobdy="";
        var width =getWidth(colnum)

        for(var i=0;i<colnum;i++){
             thead = thead + "<th width='"+width+"'></th>";
        }
        thead="<thead><tr>"+thead+"</tr></thead> \n";

        var tdId= 1;
        for(var i=0;i<rownum;i++){ //行
            tobdy=tobdy+"<tr> \n"
            for(var j=0;j<colnum;j++){ //列
                tobdy=tobdy+"<td id='tdId_"+tdId+"' height='310' ></td>";
                tdId++;
            }
            tobdy=tobdy+"</tr> \n";
        }
        tobdy= "<tbody id=\"tbody\" > \n"+tobdy+"</tbody> \n";
        table="<table width='100%' class='bjTable'  >\n"+thead+tobdy+"</table>";

        $("#bjMain").html(table);

        for(var i=0;i<bjList.length;i++){

            var bj= bjList[i];
            var cols= bj["COLS"];
            var dataArr=cols.split(",");//分割成字符串数组

            var begin = parseInt(dataArr[0]);
            var end = parseInt(dataArr[dataArr.length-1]);

            var minus = end-begin;
            var colspan = parseInt(minus%colnum) +1;
            var rowspan = parseInt(minus/colnum)  +1;

            for(var k=0;k<dataArr.length;k++){
                 var m = dataArr[k];
                 if(m!=begin){
                     $("#tdId_"+m).remove();
                 }
            }
            var wzid =bj["WZID"];
            $("#tdId_"+begin).attr("colspan",colspan).attr("rowspan",rowspan);
            var tbyy_id=bj["TBYY_ID"];

            if(tbyy_id!=null){
                var h =  $("#tdId_"+begin).height()-50;
                var tbyy_mc = bj["TBYY_MC"];
                var mbid = bj["MBID"];
                var url = bj["TBYY_DZ"];
                $("#tdId_"+begin).html(
                     '<div class="chartBox" id="wz_'+wzid+'"  wzid="'+wzid+'" cols="'+cols+'">\n'+
                         "<div class='gnTitle'> <span class='title-s'>"+tbyy_mc+"</span><span class='tool'><a title='替换' onclick='toaddTbyy(\""+wzid+"\",\""+mbid+"\");'><i class='fa fa-exchange fa fa-exchange fa-lg'></i></a></span></div> \n"+
                         "<iframe width='100%' height='"+h+"' frameborder='0' style='background: #f8f8f8;' src='"+url+"'></iframe> \n"+
                     '</div>'
                 );
            }else{
                 $("#tdId_"+begin).html(
                    ' <div class="chartBox" id="wz_'+wzid+'"  wzid="'+wzid+'" cols="'+cols+'">\n'+
                    '      <span class="toPlusGn"  onclick=\'toaddTbyy("'+wzid+'","'+mbid+'");\' >\n'+
                    '         <i class="fa fa-plus fa-4x" aria-hidden="true"></i><br/>\n'+
                    '        <span class="b-title-fz1">添加功能</span>\n'+
                    '      </span>\n'+
                    '  </div>');
            }


        }

     }

    for(var i=0;i<mbList.length;i++) {
        if(mbid==mbList[i]["MBID"]){
            initTable(mbList[i]["bjList"],mbList[i]["ROWN"],mbList[i]["COLN"])
            break;
        }
    }

    function loadYhMb(yhbjList){

        for(var i=0;i<yhbjList.length;i++){
            var yhbj = yhbjList[i];
            var tbyy_mc = yhbj["TBYY_MC"];
            var url = yhbj["TBYY_DZ"];
            var wzid = yhbj["WZID"];
            var tbyy_id = yhbj["TBYY_ID"];
            var obj = $("#wz_"+wzid);
            var h = $(obj).parent().height()-50;

            $(obj).attr("tbyy_id",tbyy_id).empty();
            $(obj).append(
                "<div class='gnTitle'> <span class='title-s'>"+tbyy_mc+"</span><span class='tool'><a title='替换' onclick='toaddTbyy(\""+wzid+"\",\""+mbid+"\");'><i class='fa fa-exchange fa fa-exchange fa-lg'></i></a></span></div>"
            ).append(
                "<iframe width='100%' height='"+h+"' frameborder='0' style='background: #f8f8f8;' src='"+url+"'></iframe>"
            );
        }

    }

    var yhmbList = zxdata["yhmbList"];
    for(var i=0;i<yhmbList.length;i++ ){
         if(mbid==yhmbList[i]["MBID"]){
             var bjList =yhmbList[i]["bjList"] ;
             if(bjList!=null){
                 loadYhMb(bjList);
             }
             break;
         }
    }

}


function toaddTbyy(wzid,mbid){
    $("#collectModal").modal('show');
    setTimeout(function(){
        $("#modal-iframe").attr("src","/portal/user/layout/showTbyy.do?wzid="+wzid+"&mbid="+mbid);
    },300)
}

function upWzMb(tbyy_id,wzid,mbid,tbyy_mc,url){

    var obj = $("#wz_"+wzid);
    var h = $(obj).parent().height()-50;
    $(obj).empty().append(
        "<div class='gnTitle'> <span class='title-s'>"+tbyy_mc+"</span><span class='tool'><a title='替换' onclick='toaddTbyy(\""+wzid+"\",\""+mbid+"\");'><i class='fa fa-exchange fa fa-exchange fa-lg'></i></a></span></div> \n"
    ).append(
        "<iframe width='100%' height='"+h+"' frameborder='0' style='background: #f8f8f8;' src='"+url+"'></iframe>"
    );

    $.ajax({
        url: "/portal/user/layout/getUserSet.do",
        type: "post",
        data:{},
        dataType:"json",
        async:false,
        timeout:sys_timeout,
        error:function(XMLHttpRequest, textStatus){

        },
        success:function(responseText){
            if(responseText.code==0){
                zxdata =responseText.repData;
            }
        }
    });


}

function saveLayout(){

    $.ajax({
        url: "/portal/user/layout/save.do",
        type: "post",
        data:{
            zxlx:$("input[name='zxlx']:checked").val(),
            mbid:$("#mbSelect").val()
        },
        dataType:"json",
        async:false,
        timeout:sys_timeout,
        error:function(XMLHttpRequest, textStatus){
             innerToast.error("服务器异常："+textStatus);

        },
        success:function(responseText){
            if(responseText.code==0){
                 innerToast.success('保存成功！');
            }else{
                innerToast.error(responseText.msg);

             }
        }
    });
}